package com.idroid.database;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import android.content.Context;
import android.database.SQLException;
import android.os.Looper;

import com.idroid.domain.AreaType;
import com.idroid.domain.ParkingArea;
import com.idroid.domain.PaymentMethodType;

public class DatabaseService {

	private static DatabaseService instance = null;

	private DatabaseService() {

	}

	public static synchronized DatabaseService getInstance() {
		if (instance == null) {
			instance = new DatabaseService();
		}
		return instance;
	}

	public void updateParkingArea(Context ctx, ParkingArea area) {
		UpdateAreaLocationAsyncTask task = new UpdateAreaLocationAsyncTask(ctx,
				area.getId(), area.getLatitude(), area.getLongitude());

		int returnedVal = -1;

		task.execute();

		try {
			returnedVal = task.get(5000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {

			e.printStackTrace();
		} catch (ExecutionException e) {

			e.printStackTrace();
		} catch (TimeoutException e) {

			e.printStackTrace();
		}

		System.out.println(returnedVal);

	}

	public ArrayList<ParkingArea> getParkingAreasByZipCode(Context ctx,
			int zipCode, boolean maxPriceFilterEnabled, double maxPriceFilter,
			boolean areaTypeFilterEnabled, AreaType areaFilter) {

		ArrayList<ParkingArea> parkingAreas = null;

		AreasByZipCodeAsyncTask task = new AreasByZipCodeAsyncTask(ctx,
				zipCode, maxPriceFilterEnabled, maxPriceFilter,
				areaTypeFilterEnabled, areaFilter);

		task.execute();

		try {
			parkingAreas = task.get(5000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		} catch (ExecutionException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		} catch (TimeoutException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		}

		return parkingAreas;
	}

	public ArrayList<ParkingArea> getParkingAreasByCitySate(Context ctx,
			String city, String state, boolean maxPriceFilterEnabled,
			double maxPriceFilter, boolean areaTypeFilterEnabled,
			AreaType areaFilter) {

		ArrayList<ParkingArea> parkingAreas = null;

		AreasByCityStateAsyncTask task = new AreasByCityStateAsyncTask(ctx,
				city, state, maxPriceFilterEnabled, maxPriceFilter,
				areaTypeFilterEnabled, areaFilter);

		task.execute();

		try {
			parkingAreas = task.get(5000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		} catch (ExecutionException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		} catch (TimeoutException e) {
			parkingAreas = new ArrayList<ParkingArea>();
			e.printStackTrace();
		}

		return parkingAreas;
	}

}